using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ClientTools.Client.Controls
{
    public sealed class ControlsManager
    {
        public static ControlInfoCollection GetAllUserControls(string path)
        {
            ControlInfoCollection returnValue = new ControlInfoCollection();
            foreach (string file in System.IO.Directory.GetFiles(path, "ClientTools.Client.Controls.*.dll"))
            {
                Assembly assembly = Assembly.LoadFile(file);
                foreach (Type t in assembly.GetTypes())
                {
                    if (t.IsClass)
                    {
                        Type i = t.GetInterface(typeof(IUserControl).Name);
                        if (i != null)
                        {
                            IUserControl uc = (IUserControl)Activator.CreateInstance(t);
                            ControlInfo ci = new ControlInfo();
                            ci.Sort = uc.Sort;
                            ci.TypeName = t.Name;
                            ci.Type = t;
                            ci.ButtonText = uc.ButtonText;
                            ci.ButtonImage = uc.ButtonImage;
                            ci.ShowDelimiter = uc.ShowDelimiter;
                            returnValue.Add(ci);
                        }
                    }
                }
            }
            returnValue.Sort(new ControlInfoComparer());
            return returnValue;
        }

        public static ControlInfoCollection GetAllUserControlsNoProfile(string path)
        {
            ControlInfoCollection returnValue = new ControlInfoCollection();
            foreach (string file in System.IO.Directory.GetFiles(path, "ClientTools.Client.Controls.*.dll"))
            {
                Assembly assembly = Assembly.LoadFile(file);
                foreach (Type t in assembly.GetTypes())
                {
                    if (t.IsClass)
                    {
                        Type i = t.GetInterface(typeof(IUserControlNoProfile).Name);
                        if (i != null)
                        {
                            IUserControlNoProfile uc = (IUserControlNoProfile)Activator.CreateInstance(t);
                            ControlInfo ci = new ControlInfo();
                            ci.Sort = uc.Sort;
                            ci.TypeName = t.Name;
                            ci.Type = t;
                            ci.ButtonText = uc.ButtonText;
                            ci.ButtonImage = uc.ButtonImage;
                            ci.ShowDelimiter = uc.ShowDelimiter;
                            returnValue.Add(ci);
                        }
                    }
                }
            }
            returnValue.Sort(new ControlInfoComparer());
            return returnValue;
        }
    }
}
